﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using System.Data;
using Runda.EShop.Bll;

namespace Runda.EShop.SaaS.SysManage
{
    public partial class OperationLogList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        { 
            ddlOpLogType.BindEnum(EnumDescAttribute.GetFieldTexts(typeof(EnumHelper.OperationType)));

            ddlOpLogType.Items.RemoveAt(0);
            ddlOpLogType.Items.Insert(0, new ListItem("全部", ""));

            ddlUserType.BindEnum(EnumDescAttribute.GetFieldTexts(typeof(EnumHelper.UserType)));
            ddlUserType.Items.Insert(0, new ListItem("全部", ""));

        }

        protected void lnkbtnQuery_Click(object sender, EventArgs e)
        {
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }

        protected void gvLogList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblUserType = e.Row.FindControl("lblUserType") as Label;
                Label lblOperationType = e.Row.FindControl("lblOperationType") as Label;

                SysLogOperationEntity entity = e.Row.DataItem as SysLogOperationEntity;

                lblUserType.Text = EnumDescAttribute.GetFieldText((EnumHelper.UserType)entity.UserType);
                lblOperationType.Text = EnumDescAttribute.GetFieldText((EnumHelper.OperationType)entity.OperationType);
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;
            if (!string.IsNullOrEmpty(ddlUserType.SelectedValue))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[UserType]";
                parm.ColumnType = DbType.Int32;
                parm.ParameterName = "@UserType";
                parm.ParameterValue = Convert.ToInt32(ddlUserType.SelectedValue);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            if (!string.IsNullOrEmpty(ddlOpLogType.SelectedValue))
            {
                parm = new SqlDbParameter();
                parm.ColumnName = "[OperationType]";
                parm.ColumnType = DbType.Int32;
                parm.ParameterName = "@OperationType";
                parm.ParameterValue = Convert.ToInt32(ddlOpLogType.SelectedValue);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
            }

            int RecordCount = 0;
            List<SysLogOperationEntity> list = new SystemBll().GetAll<SysLogOperationEntity>(parms, "CreatedAt DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvLogList.DataSource = list;
            gvLogList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

    }
}